let webpack = require('webpack');
let path = require('path');
var NpmInstallPlugin = require('npm-install-webpack-plugin');
let entry = require('./entry.js');
const pathConfig = require('./config.json');

let paths = {
    OUTPUT: path.resolve(__dirname, pathConfig.output)
};
//#region jsmodule
var jsContainer = {};
entry("", /(.+)\.js$/, jsContainer);
for (var common of pathConfig.common) {
    for (var index in common) {
        jsContainer[index] = common[index];
    }
}

let config = {
    plugins: [],
    module: {
        rules: []
    },
    entry: jsContainer,
    output: {
        path: paths.OUTPUT,
        filename: '[name].js',
        publicPath:pathConfig.publicPath
    }
};
//#region Plugins

let uglifyJsPluginOptions = {
    parallel: true,
    uglifyOptions: {
        ie8: true,
        ecma: 8,
        output: {
            comments: true,
            beautify: true
        },
        warnings: false,
        compress: true
    }
};
let uglifyJsPlugin = new webpack.optimize.UglifyJsPlugin(uglifyJsPluginOptions);
let npmInstallPlugin = new NpmInstallPlugin({
    // 使用 --save 或者 --save-dev
    dev: false,
    // 安装缺少的 peerDependencies
    peerDependencies: true,
    // 减少控制台日志记录的数量
    quiet: false,
    // npm command used inside company, yarn is not supported yet
    npm: 'npm'
});

let commonPlugin = new webpack.optimize.CommonsChunkPlugin({
  name: "js/vendor",
  filename:"js/vendor.js"
})

config.plugins.push(uglifyJsPlugin);
config.plugins.push(commonPlugin);
config.plugins.push(npmInstallPlugin);
//config.devtool="cheap-module-eval-source-map";
//#endregion

//#region Module
let babelLoader = {
    test: /\.js$/,
    loader: 'babel-loader',
    exclude: /(node_modules|bower_components)/,
    query: pathConfig.query
};
let json5Loader = {
    test: /\.json5$/,
    loader: 'json5-loader'
};
let vueLoader ={
    test: /\.vue$/,   
    loader: 'vue-loader',
    exclude: /node_modules/
}
config.module.rules.push(babelLoader);
config.module.rules.push(json5Loader);
config.module.rules.push(vueLoader);

config.resolve = {
	
    alias: {
        'vue': 'vue/dist/vue.common.js'
    }
};

//#endregion
//#endregion

module.exports = config;